package com.erp.erp_ui.Dictionary;

import com.erp.erp_entitys.DictionaryKey.DictionaryKeyEntity;

import com.erp.erp_entitys.DictionaryKey.DictionaryTreeEntity;
import com.erp.erp_entitys.DictionaryValue.DictionaryValueEntity;
import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_servers.Dictionary.IDictionaryService;
import com.erp.erp_servers.Dictionary.IDictionaryValueService;
import com.erp.erp_ui.base.Base;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName DictionaryKeyControl
 * @Description 字典类
 * @Author songjizhen
 * @Date 2020-06-22 11:38
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "dictionary")
 public class DictionaryControl extends Base {
    @Autowired
     IDictionaryService dictionaryService;
    @Autowired
     IDictionaryValueService dictionaryValueService;
    /**
     * 根据code获取字典key值
     * @param code
     * @return
     */
    @RequestMapping(value = "selectKeyByCode",method = RequestMethod.POST)
    @ResponseBody

    public R<DictionaryKeyEntity> selectKeyByCode(int code){
        DictionaryKeyEntity de= new DictionaryKeyEntity();
        try {
            de=dictionaryService.selectKeyByCode(code);
            return R.build(de);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 根据主键更新字典
     * @param record
     * @return
     */
    @RequestMapping(value = "updateKeyByCode",method = RequestMethod.POST)
    @ResponseBody

    public R<Integer> updateKeyByCode(@RequestBody DictionaryKeyEntity record) {
        try {
            int num=dictionaryService.updateKeyByCode(record);
            return R.build( num);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 插入字典数据
     * @param record
     * @return
     */
    @RequestMapping(value = "insertKey",method = RequestMethod.POST)
    @ResponseBody

    public R<Integer> insertKey(@RequestBody DictionaryKeyEntity record){
       try {
           return R.build(dictionaryService.insertKey(record));
       }catch (Exception e){
           return R.buildError(ResultStatusEnum.ERROR);
       }
    }

    /**
     * 获取所有字典
     * @return
     */
    @RequestMapping(value = "selectKeyAll",method = RequestMethod.POST)
    @ResponseBody
     public R<List<DictionaryKeyEntity>> selectKeyAll(){
        try {
            return R.build(dictionaryService.selectKeyAll());
        }catch (Exception e){
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 获取父节点下所有子节点
     * @param code
     * @return
     */
    @RequestMapping(value = "selectKeyByParent",method = RequestMethod.POST)
    @ResponseBody

    public R<List<DictionaryKeyEntity>> selectKeyByParent(int code){
        try {
            return R.build(dictionaryService.selectKeyByParent(code));
        }catch (Exception e){
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }


    /**
     * 获取所有叶子节点的值，即右侧显示的表格内容
     * @param code
     * @return
     */
    @RequestMapping(value = "selectValueByParent",method = RequestMethod.POST)
    @ResponseBody

    public R<List<DictionaryValueEntity>> selectValueByParent(int code){
        try {
           String codes=dictionaryService.selectCodesByParent(code);
            return R.build(dictionaryValueService.selectValueByParent(codes));

        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 更新父节点下所有叶子节点的值，即更新右侧的一条数据
     * @param list
     * @return
     */
    @RequestMapping(value = "updateValueByParent",method = RequestMethod.POST)
    @ResponseBody

    public R<Integer> updateValueByParent(@RequestBody List<DictionaryValueEntity> list){
        try {
            for(int i=0;i<list.size();i++){
                DictionaryValueEntity dve=list.get(i);
                dve.setUpdatetime(new Date());
            }
            return R.build(dictionaryValueService.updateValueByParent(list));
        }catch (Exception e){
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 插入父节点下所有叶子节点的值，即右侧显示的一条数据
     * @param list
     * @return
     */
    @RequestMapping(value = "insertValueByParent",method = RequestMethod.POST)
    @ResponseBody

    public R<Integer> insertValueByParent(@RequestBody List<DictionaryValueEntity> list){
        try {
            UserEntity user = (UserEntity)this.getBean("user");
            for(int i=0;i<list.size();i++){
                DictionaryValueEntity dve=list.get(i);
                dve.setCreateor(user.getCode());
                dve.setCreatetime(new Date());
            }
            return R.build(dictionaryValueService.insertValueByParent(list));
        }catch (Exception e){
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 根据父节点（即叶子节点的父节点）,获取右侧的所有数据
     * @param code
     * @return
     */
    @RequestMapping(value = "getDictionryValueByKey",method = RequestMethod.POST)
    @ResponseBody

    public R<List<Map>> getDictionryValueByKey(int code){
        try {
            String codes=dictionaryService.selectCodesByParent(code);
            List<DictionaryValueEntity> dictionaryValueEntities = dictionaryValueService.selectValueByParent(codes);
            List<Map> list = dictionaryValueService.getMap( codes,dictionaryValueEntities);
            return R.build(list);
        }catch (Exception e){
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 获了字典树
     * @return
     */
    @RequestMapping(value = "getDictionryTree",method = RequestMethod.POST)
    @ResponseBody

    public R<DictionaryTreeEntity> getDictionryTree(){
        try {
            DictionaryTreeEntity dictionryTree = dictionaryService.getDictionryTree();
            return R.build(dictionryTree);
        }catch (Exception e){
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

}
